//
//  RoundTableView.m
//  B段项目
//
//  Created by dllo on 15/9/24.
//  Copyright (c) 2015年 昶博. All rights reserved.
//

#import "RoundTableView.h"
#import "PrefixHeader.pch"
#import "FirstCell.h"
#import "FirstModel.h"
#import "RoundModel.h"
#import "AFNetWorkingW.h"
#import "FirstView.h"
#import <UIImageView+WebCache.h>
#import <MJRefresh.h>


@interface RoundTableView () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableDictionary *mDic;
@property (nonatomic, strong) NSMutableArray *mArr;
@property (nonatomic, strong) FirstModel *firstModel;
@property (nonatomic, strong) FirstView *firstView;
@property (nonatomic, strong) NSMutableArray *imageArr;
@property (nonatomic, strong) NSMutableArray *urlArr;
@property (nonatomic, strong) RoundModel *roundModel;
@property (nonatomic, assign) NSInteger limit;
@property (nonatomic, strong) UIActivityIndicatorView *activity;

@end

@implementation RoundTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        /* 设置UITableView */
        //        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.showsVerticalScrollIndicator = NO;
        //        self.bounces = NO;
        self.delegate = self;
        self.dataSource = self;
        [self registerClass:[FirstCell class] forCellReuseIdentifier:@"reuse"];
        
        self.mArr = [NSMutableArray array];
        
        [self scrollViewDataHandle];
        
        [self setExtraCellLineHidden:self];
        
        
        self.activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        //        self.activity.center = self.center;
        self.activity.frame = CGRectMake((ScreenWidth - 80) / 2, (ScreenHeight - 120) / 2, 80, 80);
        self.activity.color = RGB(22, 188, 180);
        [self addSubview:self.activity];
        [self.activity startAnimating];
        
        [self setupRefresh];
        [self dataHandle];
        self.limit = 10;
        
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.firstModel = [self.mArr objectAtIndex:indexPath.row];
    
    FirstCell *firstCell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    
    firstCell.firstModel = self.firstModel;
    
    return firstCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ScreenHeight * 100 / 667;
}

- (void)dataHandle
{
    [AFNetWorkingW netWorkingHanderGetGataWithUrl:[NSString stringWithFormat:@"http://a.xcar.com.cn/interface/6.0/getNewsList.php?limit=%ld&offset=0&type=%d&ver=6.1.6", (long)                           self.limit, 1] resultBlock:^(id result) {
        self.mDic = result;
        NSArray *arr = [self.mDic objectForKey:@"newsList"];
        for (NSDictionary *dic in arr) {
            self.firstModel = [[FirstModel alloc] init];
            [self.firstModel setValuesForKeysWithDictionary:dic];
            
            [self.mArr addObject:self.firstModel];
            [self.footer endRefreshing];
        }
        [self reloadData];
        [self.activity stopAnimating];
    }];
    self.limit += 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return ScreenHeight / 10 * 3;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return self.firstView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    self.firstModel = [self.mArr objectAtIndex:indexPath.row];
    [center postNotificationName:@"webLink" object:self.firstModel];
}

/* 分割线隐藏 */
- (void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view =[ [UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}

- (void)scrollViewSubviews
{
    NSMutableArray *viewsArray = [@[] mutableCopy];
    for (int i = 0; i < 3; ++i) {
        UIImageView *scrollImageView = [[UIImageView alloc] init];
        [scrollImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArr[i]]  placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
        scrollImageView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight / 10 * 3);
        [viewsArray addObject:scrollImageView];
    }
    self.firstView = [[FirstView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight / 10 * 3) animationDuration:3];
    self.firstView.fetchContentViewAtIndex = ^UIView *(NSInteger pageIndex){
        return viewsArray[pageIndex];
    };
    self.firstView.totalPagesCount = ^NSInteger(void){
        return 3;
    };
    self.firstView.TapActionBlock = ^(NSInteger pageIndex){
        NSLog(@"轮播图");
    };
    [self addSubview:self.firstView];
}

- (void)scrollViewDataHandle
{
    [AFNetWorkingW netWorkingHanderGetGataWithUrl:@"http://a.xcar.com.cn/interface/6.0/getNewsList.php?limit=10&offset=0&type=1&ver=6.1.6" resultBlock:^(id result) {
        NSDictionary *dic = [result objectForKey:@"focusList"];
        NSArray *arr = [dic objectForKey:@"focusImgs"];
        self.imageArr = [NSMutableArray array];
        self.urlArr = [NSMutableArray array];
        for (int i = 0; i < 3; i++) {
            self.roundModel = [[RoundModel alloc] init];
            NSString *urlStr = [arr[i] objectForKey:@"imgURL"];
            NSString *newsLink = [arr[i] objectForKey:@"newsLink"];
            NSString *webLink = [arr[i] objectForKey:@"webLink"];
            [self.imageArr addObject:urlStr];
            [self.urlArr addObject:webLink];
            [self.roundModel setImgURL:urlStr];
            [self.roundModel setNewsLink:newsLink];
            [self.roundModel setWebLink:webLink];

        }
        [self scrollViewSubviews];
        [self reloadData];
    }];
}

#pragma mark ** 刷新
- (void)setupRefresh
{
    /* 上拉刷新 */
    self.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefreshing)];
    [self.header beginRefreshing];
    /* 下拉加载数据 */
    self.footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefreshing)];
    [self.footer beginRefreshing];
}

- (void)headerRefreshing
{
    [self reloadData];
    [self.header endRefreshing];
}

- (void)footerRefreshing
{
    [self reloadData];
    [self.footer endRefreshing];
    [self dataHandle];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
